var t = getApp();

Page({
    data: {
        model: t.home,
        home_intro: t.home_intro,
        showModel: t.home,
        tokenSwitch: t.tokenSwitch,
        canNavHome: t.canNavHome,
        pageData: {},
        card: {},
        leftBtnTxt: "",
        icon: "",
        server: t.rserver,
        rserver: "http://www.ylhzzy.top:8828/public",
        serverUrl: t.rserverUrl,
        rserverUrl: "http://www.ylhzzy.top:8828/public",
        show: !1,
        menuVisible: !1,
        agentToken: "",
        currAgentToken: "",
        force_producer: 0,
        show_btn: 0,
        title: "首页",
        foceHome: !1,
        newsActive: !1,
        news: [],
        newsSize: 10,
        newsPage: 1,
        newsTotal: 0,
        newsLoading: !1,
        newsHashMore: !1,
        srolltop: 0,
        series: [],
        lv1: 0,
        lv2: 0
    },
    onLoad: function(e) {
        var a = this, o = e.at ? decodeURIComponent(e.at) : "";
        o && this.setData({
            agentToken: o,
            force_producer: Number(e.f) || 0
        }), this.setData({
            title: "card" === this.data.model ? "名片" : "首页"
        }), wx.setNavigationBarTitle({
            title: "card" === this.data.model ? "名片" : "首页"
        });
        var n = this;
        t.request({
            url: t.rserver + "/userApi/getFactoryInfo",
            method: "POST",
            data: {
                token: t.token
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                1 == t.data.result.info.close_watermark ? wx.setStorage({
                    key: "watermark",
                    data: t.data.result.info.watermark
                }) : wx.setStorage({
                    key: "watermark",
                    data: ""
                });
                var e = t.data.result.info.home;
                e !== a.data.model && (a.setData({
                    model: e,
                    showModel: e
                }), wx.setStorage({
                    key: "home",
                    data: e
                }), console.log(a.data.home)), a.setData({
                    pageData: t.data.result.info
                });
                for (var o = [], n = 0; n < t.data.result.info.module.length; n++) {
                    var r = t.data.result.info.module[n], s = {
                        txt: "",
                        children: []
                    };
                    if (r.children) {
                        s.txt = r.title;
                        for (var i = 0; i < r.children.length; i++) {
                            var d = r.children[i], l = {
                                txt: "",
                                pic: ""
                            };
                            if (l.txt = d.title, 0 != d.info.length && (l.pic = d.info[0].pic), d.info) {
                                var c = [], h = 0, u = !0, f = !1, w = void 0;
                                try {
                                    for (var g, m = d.info[Symbol.iterator](); !(u = (g = m.next()).done); u = !0) {
                                        var v = g.value, p = JSON.parse(v.info_content), y = !0, k = !1, x = void 0;
                                        try {
                                            for (var T, _ = p[Symbol.iterator](); !(y = (T = _.next()).done); y = !0) {
                                                var D = T.value;
                                                for (var S in D.attr) {
                                                    var b = D.attr[S];
                                                    b && (b *= .39), D[S] = b;
                                                }
                                                h = Math.max(h, D.y + D.height), c.push(D);
                                            }
                                        } catch (t) {
                                            k = !0, x = t;
                                        } finally {
                                            try {
                                                !y && _.return && _.return();
                                            } finally {
                                                if (k) throw x;
                                            }
                                        }
                                    }
                                } catch (t) {
                                    f = !0, w = t;
                                } finally {
                                    try {
                                        !u && m.return && m.return();
                                    } finally {
                                        if (f) throw w;
                                    }
                                }
                                l.max = h, l.info = c;
                            }
                            s.children.push(l);
                        }
                    } else s.children.push([]);
                    o.push(s);
                }
                "card" !== a.data.model && a.reloadNews(), a.setData({
                    series: o
                }), a.changeTitle();
            }
        });
    },
    switchTab: function(t) {
        this.setData({
            srolltop: 0,
            lv2: t.detail.current
        });
    },
    cardTap: function(t) {
        switch (t.detail.btn) {
          case "left":
            this.data.leftBtnTxt && ("厂家名片" === this.data.leftBtnTxt ? this.setData({
                force_producer: 1
            }) : this.setData({
                force_producer: 0
            }), this.setData({
                agentToken: ""
            }), this.loadCard());
            break;

          case "center":
            this.sendCard();
            break;

          case "right":
            this.data.canNavHome && (this.setData({
                foceHome: !0
            }), this.changeTitle());
        }
    },
    tocard: function() {
        this.setData({
            show: !1,
            foceHome: !1
        }), this.changeTitle();
    },
    changeMenuVisible: function() {
        this.setData({
            menuVisible: !this.data.menuVisible
        });
    },
    loadUsr: function() {
        var t = wx.getStorageSync("usr");
        this.setData({
            usr: t
        });
    },
    changeTitle: function() {
        var t = "", e = this.data.model;
        this.data.foceHome || "card" !== this.data.model && 0 !== this.data.card_type && 1 !== this.data.card_type ? (e = "home", 
        t = "首页") : (e = "card", 2 === this.data.card_type ? t = this.data.pageData.company_name || "" : 0 !== this.data.card_type && 1 !== this.data.card_type || (t = this.data.card.name || ""), 
        t || (t = "名片")), this.setData({
            title: t,
            showModel: e
        }), wx.setNavigationBarTitle({
            title: t
        });
    },
    loadCard: function() {
        var e = this;
        this.loadUsr();
        var a = "";
        this.data.agentToken ? a = this.data.agentToken : this.data.usr.agent_token && (a = this.data.usr.agent_token), 
        this.setData({
            currAgentToken: a
        }), t.request({
            url: t.server + "/userApi/getVisitingCard",
            method: "POST",
            data: {
                token: t.token,
                agent_token: a,
                userToken: this.data.usr ? this.data.usr.userToken : "",
                force_producer: this.data.force_producer
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var a = "";
                0 === t.data.code ? (1 === t.data.result.show_btn ? a = "我的名片" : 2 === t.data.result.show_btn && (a = "厂家名片"), 
                e.setData({
                    card: t.data.result.user,
                    show_btn: t.data.result.show_btn,
                    card_type: t.data.result.card_type,
                    leftBtnTxt: a
                }), e.changeTitle()) : (e.setData({
                    card: {},
                    show_btn: 0,
                    leftBtnTxt: ""
                }), e.changeTitle());
            }
        });
    },
    onReady: function() {},
    onShow: function() {
        this.loadCard();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        var t = encodeURIComponent(this.data.currAgentToken);
        return {
            title: this.data.title,
            path: "pages/index/index?at=" + t + "&f=" + this.data.force_producer,
            success: function(t) {
                console.log("转发成功:" + JSON.stringify(t));
            },
            fail: function(t) {
                console.log("转发失败:" + JSON.stringify(t));
            }
        };
    },
    showIntroduce: function() {
        this.setData({
            show: !this.data.show
        });
    },
    viewTouchMove: function(t) {
        this.setData({
            left: t.touches[0].clientX - 30,
            top: t.touches[0].clientY - 15
        });
    },
    toMem: function() {
        t.modules.video ? wx.navigateTo({
            url: "/pages/member/member"
        }) : wx.switchTab({
            url: "/pages/member/member"
        });
    },
    changeLv1: function(t) {
        this.setData({
            newsActive: !1,
            lv1: t.currentTarget.dataset.lv1,
            lv2: 0,
            srolltop: 0
        });
    },
    changeLv2: function(t) {
        this.setData({
            lv2: t.currentTarget.dataset.lv2,
            srolltop: 0
        });
    },
    change2news: function() {
        this.setData({
            newsActive: !0
        });
    },
    reloadNews: function() {
        console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!22");
        var e = this;
        this.setData({
            newsLoading: !1
        }), 
        t.request({
            url: t.rserver + "/newsApi/getList",
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                token: t.token,
                page_size: this.data.newsSize,
                page: this.data.newsPage
            },
            success: function(t) {
                console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
                console.log(t);
                var a = !0, o = !1, n = void 0;
                try {
                    for (var r, s = t.data.result.list[Symbol.iterator](); !(a = (r = s.next()).done); a = !0) {
                        var i = r.value;
                        e.data.news.push(i);
                    }
                } catch (t) {
                    console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!444");
                    o = !0, n = t;
                } finally {
                    console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!666");
                    try {
                        !a && s.return && s.return();
                    } finally {
                        if (o) throw n;
                    }
                }
                e.setData({
                    newsHashMore: t.data.result.paginate.total_page > e.data.newsPage,
                    newsLoading: !1,
                    news: e.data.news
                });
            }
        });
    },
    loadNextNews: function() {
        this.data.newsHashMore && !this.data.newsLoading && (this.setData({
            newsPage: this.data.newsPage + 1
        }), this.reloadNews());
    }
});